package com.qire.manhua.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.qire.common.utils.EquipmentInfoUtil;
import com.qire.manhua.R;

import androidx.annotation.Nullable;

public class IndicatorView extends LinearLayout {

    private int      indicatorPointCount        = 0;
//    private int      currentIndicatorPointIndex = -1;
    private int      indicatorDrawableRes;
    private Drawable indicatorDrawable ;

    public IndicatorView(Context context) {
        super(context);
        init();
    }

    public IndicatorView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public IndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        setIndicatorDrawable(R.drawable.app_home_bookshelf_indicator_point);
    }

    public void setIndicatorDrawable(int resid){
        if (resid != 0 && resid == indicatorDrawableRes) {
            return;
        }
        Drawable d = null;
        if (resid != 0) {
            d = getContext().getResources().getDrawable(resid);
        }
        setIndicatorDrawable(d);
        indicatorDrawableRes = resid;
    }

    public void setIndicatorDrawable(Drawable indicatorDrawable){
        this.indicatorDrawable = indicatorDrawable;
        GradientDrawable gradientDrawable = (GradientDrawable) indicatorDrawable;
        gradientDrawable.setColor(Color.argb(0x33,0xff,0xca,0x2b));
    }

    public void setCurrentIndicatorPoint(int position) {

        if(getChildCount()<=0)
            return;
        if(position>indicatorPointCount)
            return;

        for(int index=0;index<indicatorPointCount;index++){
            int color = index == position ? Color.argb(0xff,0xff,0xca,0x2b) : Color.argb(0x33,0xff,0xca,0x2b);
            ((GradientDrawable) getChildAt(index).getBackground()).setColor(color);
        }

//        GradientDrawable gradientDrawable = (GradientDrawable) getChildAt(position).getBackground();
//        gradientDrawable.setColor(Color.argb(0xff,0xff,0xca,0x2b));
//
//        if(currentIndicatorPointIndex > -1 && currentIndicatorPointIndex < indicatorPointCount ) {
//            GradientDrawable gradientDrawable2 = (GradientDrawable) getChildAt(currentIndicatorPointIndex).getBackground();
//            gradientDrawable2.setColor(Color.argb(0x33,0xff,0xca,0x2b));
//        }
//
//        currentIndicatorPointIndex = position;
    }

    public void setIndicatorPointCount(int pointCount){
        if(pointCount == indicatorPointCount)
            return;

        this.removeAllViews();
        indicatorPointCount = 0;
        for(int index=0;index<pointCount;index++){
            addPoint();
            indicatorPointCount++;
        }
    }

    private void addPoint() {
        int dp_5   = EquipmentInfoUtil.dip2px(5);
        int margin = EquipmentInfoUtil.dip2px(3);

        LayoutParams layoutParams = new LayoutParams(dp_5, dp_5);
        layoutParams.setMargins(margin, 0, margin, 0);

        View pointImageView = new View(getContext());
        pointImageView.setLayoutParams(layoutParams);
        pointImageView.setBackground(indicatorDrawable.getConstantState().newDrawable());

        addView(pointImageView);
    }

}
